package com.wizardOther.Items;

import static com.appclient.Start.stage;
import com.wizardOther.OtherControlledScreen;
import com.wizardOther.OtherScreensController;
import com.wizardOther.OtherScreensFramework;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import com.backup.BackupRestore;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.EventHandler;
import javafx.stage.FileChooser;

/**
 *
 * @author Vi
 */
public class BackUpController extends ValidationForm implements Initializable, OtherControlledScreen {

    private OtherScreensController myController;
    private BackupRestore backup = new BackupRestore();
    private FileChooser fileChooser;
    private File file;

    @FXML
    private TextField pathBackUp;
    @FXML
    private Label pathError;
    @FXML
    private Button choosePathToSave;
    @FXML
    private Button saveNewCopy;

    @FXML
    private TextField pathRestore;
    @FXML
    private Label restorePathError;
    @FXML
    private Button choosePathToRestore;
    @FXML
    private Button restoreCopy;
    
    @FXML 
    private Button cancel;

    private String pathBackupDirectory;
    private ArrayList<Node> inputs = new ArrayList<Node>();
    private ArrayList<Label> labels = new ArrayList<Label>();

    @FXML
    private void handleButtonAction(ActionEvent event) {

        if (event.getSource() == choosePathToSave) {

            createFileChooer();
            file = fileChooser.showSaveDialog(stage);
            if (file != null) {
                pathBackUp.setText(file.getAbsolutePath());
                backup.setBackupPath(file.getAbsolutePath());
            }

        }
        if (event.getSource() == saveNewCopy) {
            if(this.pathBackUp.getText().isEmpty()){
                createPopup("Musisz wybrać ściężkę!");
            }
            else{
                backup.backupDB();
                
            }
        }
        if (event.getSource() == choosePathToRestore) {
            createFileChooer();
            file = fileChooser.showOpenDialog(stage);
            if (file != null) {
                pathRestore.setText(file.getAbsolutePath());
                backup.setRestorePath(file.getAbsolutePath());
            }
        }
        if (event.getSource() == restoreCopy) {
            
            if(this.pathRestore.getText().isEmpty()){
                createPopup("Musisz wybrać ścieżkę!");
            }
            else{
                backup.restoreDB();
            }
        }
        
        if(event.getSource()==cancel){
            this.pathBackUp.setText("");
            this.pathRestore.setText("");
        }

    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        inputs.add(pathBackUp);
        inputs.add(pathRestore);

        labels.add(pathError);
        labels.add(restorePathError);

    }

    @Override
    public void setScreenParent(OtherScreensController screenParent) {
        myController = screenParent;
    }

    public void createFileChooer() {

        fileChooser = new FileChooser();

        //Set extension filter
        FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("SQL (*.sql)", "*.sql");
        fileChooser.getExtensionFilters().add(extFilter);
    }

}
